﻿using Furion;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using VueEleAdminFurion.Controllers.RoleServices.Dto;
using VueEleAdminFurion.Model;

namespace VueEleAdminFurion.Controllers.RoleServices;

[DynamicApiController]
public class RoleService
{
    ISqlSugarClient db;
    public RoleService(ISqlSugarClient db)
    {
        this.db = db;
    }
    public async ValueTask CreateRole(RoleDto dto)
    {
        dto.Id = Guid.NewGuid();
        dto.CreateUserId = Guid.Parse(input: App.User?.FindFirstValue("UserId"));
        dto.CreateTime = db.GetDate().ToString();
        db.Insertable<Role>(dto).IgnoreColumns(it => new { it.TimeStamp }).ExecuteCommand();
        if (dto.RolePermissionsIds.Any())
        {
            db.Deleteable<RolePermissions>().Where(it => it.RoleId == dto.Id).ExecuteCommand();
            dto.RolePermissionsIds.ForEach(x =>
                db.Insertable(new RolePermissions()
                {
                    PermissionsId = Guid.Parse(x),
                    RoleId = dto.Id,
                    Id = Guid.NewGuid(),
                }).IgnoreColumns(it => it.TimeStamp).ExecuteCommand()
            );
        }
    }
    public async ValueTask DeleteRole([FromQuery] Role role)
    {
        await db.Deleteable<Role>().Where(it => it.Id == role.Id).ExecuteCommandAsync();
        await db.Deleteable<UserRole>().Where(it => it.RoleId == role.Id).ExecuteCommandAsync();
        await db.Deleteable<RolePermissions>().Where(it => it.RoleId == role.Id).ExecuteCommandAsync();
    }
    public async ValueTask UpdateRole(RoleDto dto)
    {
        var ss = db.Updateable<Role>(dto).IgnoreColumns(it => new { it.CreateTime, it.TimeStamp }).ExecuteCommand();

        if (dto.RolePermissionsIds.Any())
        {
            db.Deleteable<RolePermissions>().Where(it => it.RoleId == dto.Id).ExecuteCommand();
            dto.RolePermissionsIds.ForEach(x =>
                db.Insertable(new RolePermissions()
                {
                    PermissionsId = Guid.Parse(x),
                    RoleId = dto.Id,
                    Id = Guid.NewGuid(),
                }).IgnoreColumns(it => it.TimeStamp).ExecuteCommand()
            );
        }
    }
    public async ValueTask<dynamic> GetRoleByPage([FromQuery] RoleByPageDto dto)
    {
        try
        {
            int totalCount = 0;
            var role = db.Queryable<Role>().ToPageList(dto.PageIndex, dto.PageSize, ref totalCount).Where(it => it.RoleName.Contains(dto.RoleName ?? "") && it.RoleDescribe.Contains(dto.RoleDescribe ?? "")).ToList();
            return new { total = totalCount, data = role };

        }
        catch (Exception ex) { throw new Exception(ex.Message); }
    }
    public async ValueTask<List<Role>> GetAllRole() => db.Queryable<Role>().ToList();

    public async ValueTask<List<Guid>> GetRolePermissionsIds([FromQuery] Guid id)
     => db.Queryable<RolePermissions>().Where(it => it.RoleId == id).Select(it => it.PermissionsId).ToList();
}